package com.leaves.leetcode.array.t1343_NumOfSubarrays;


/**
 * 核心要点：
 * 固定长度滑动窗口
**/
 class Solution {
    /**
     * 固定长度滑动窗口
     * 时间复杂度：O(n)
     * 空间复杂度：O(1)
     * @param arr
     * @param k
     * @param threshold
     * @return
     */
    public int numOfSubarrays(int[] arr, int k, int threshold) {
        int ans = 0, n = arr.length, thresholdSum = k*threshold;
        int subSum = 0;
        for (int i =0; i < k; i++) subSum += arr[i];
        ans += subSum >= thresholdSum ? 1 : 0;

        for (int i = k; i < n; i++) {
            subSum += arr[i] - arr[i - k];
            ans += subSum >= thresholdSum ? 1 : 0;
        }

        return ans;
    }
 }